package com.ruoyi.project.instrument.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.framework.mqtt.gateway.IMqttSender;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.instrument.domain.InsInstrument;
import com.ruoyi.project.instrument.mapper.InsInstrumentMapper;
import com.ruoyi.project.instrument.service.IInsInstrumentService;
import com.ruoyi.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 仪器信息Service业务层处理
 *
 * @author taney
 * @date 2020-06-08
 */
@Service
public class InsInstrumentServiceImpl extends ServiceImpl<InsInstrumentMapper, InsInstrument> implements IInsInstrumentService {
    /**
     * 注入发送MQTT的Bean
     */
    @Resource
    private IMqttSender iMqttSender;
    @Autowired
    IInsInstrumentService insInstrumentService;
    @Autowired
    ISysDictDataService dictDataService;

    @Override
    public AjaxResult calibration(List<Integer> asList) {
        asList.forEach((deviceId -> {
            GetDeviceInfo device = new GetDeviceInfo(deviceId).invoke();
            iMqttSender.sendToMqtt("/device/" + device.getModelDesc() + "/" + device.getDeviceName() + "/properties/write", "calibration");
        }));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxResult versionUpdate(List<Integer> asList) {
        asList.forEach((deviceId -> {
            GetDeviceInfo device = new GetDeviceInfo(deviceId).invoke();
            iMqttSender.sendToMqtt("/device/" + device.getModelDesc() + "/" + device.getDeviceName() + "/function/invoke", "1");
        }));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxResult powerOff(List<Integer> asList, String state, String netState) {
        InsInstrument insInstrument = new InsInstrument();
        asList.forEach(deviceId -> {
            GetDeviceInfo device = new GetDeviceInfo(deviceId).invoke();
            iMqttSender.sendToMqtt("/device/" + device.getModelDesc() + "/" + device.getDeviceName() + "/function/invoke", "2");
            insInstrument.setId(deviceId);
            insInstrument.setState(state);
            insInstrument.setNetState(netState);
            updateById(insInstrument);
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    private class GetDeviceInfo {
        private Integer deviceId;
        private String modelDesc;
        private String deviceName;

        public GetDeviceInfo(Integer deviceId) {
            this.deviceId = deviceId;
        }

        public String getModelDesc() {
            return modelDesc;
        }

        public String getDeviceName() {
            return deviceName;
        }

        public GetDeviceInfo invoke() {
            InsInstrument instrument = insInstrumentService.getById(deviceId);
            modelDesc = dictDataService.selectDictLabel("machine_type", instrument.getModel());
            deviceName = instrument.getName();
            return this;
        }
    }
}
